package _03_链表;

import _01_第一季._00_公共接口.AbstractList;
/*
    双向链表
    gc root对象:被栈指针指向的对象（被局部变量所指向的对象）；只要被它指向的对象存在，就不会被垃圾回收器回收

    添加：需要考虑尾节点和头节点的情况
    删除：需要考虑头尾节点的情况
 */
public class LinkedList<E> extends AbstractList<E>{
    private Node<E> first;
    private Node<E> last;

    private static class Node<E>{
        E element;
        Node<E> prev;
        Node<E> next;

        public Node(){
            this(null,null,null);
        }

        public Node(E element, Node<E> prev, Node<E> next) {
            this.element = element;
            this.prev = prev;
            this.next = next;
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();

            if(prev!=null){
                sb.append(prev.element);
            }else{
                sb.append("null");
            }
            sb.append("_").append(element).append("_");

            if(next!=null){
                sb.append(next.element);
            }else {
                sb.append("null");
            }
            return sb.toString();
        }
    }

    private Node<E> node(int index){
        rangeCheck(index);

        if(index < (size >> 1)){
            Node<E> node = first;
            for(int i=0;i<index;++i){
                node = node.next;
            }
            return node;
        }else{
            Node<E> node = last;
            for(int i=size-1;i>index;--i){
                node = node.prev;
            }
            return node;
        }
    }

    //在指定索引添加元素
    @Override
    public void add(int index, E element) {
        rangeCheckForAdd(index);

       if(index == size){ //在尾节点添加
           Node<E> oldLast = last;
           last = new Node<>(element,oldLast,null);
           if(oldLast == null){ //当链表为空的时候
               first = last;
           }else{
               oldLast.next = last;
           }
       }else{ //在中间和开头添加
           Node<E> next = node(index);
           Node<E> prev = next.prev;
           Node<E> node = new Node<>(element,prev,next);
           next.prev = node;
           if(prev == null){ // index = 0,在头节点添加
               first = node;
           }else{
               prev.next = node;
           }
       }
        size++;
    }

    @Override
    public E get(int index) {
        return node(index).element;
    }

    @Override
    public E set(int index, E element) {
        Node<E> node = node(index);
        E ret = node.element; //保存之前的信息
        node.element = element; //进行修改
        return ret;
    }

    @Override
    public void remove(E element) {
        int index = indexOf(element);
        if(index!=ELEMENT_NOT_FOUND){
            remove(index);
        }
    }

    //删除指定索引的元素
    @Override
    public E remove(int index) {
        rangeCheck(index);
        Node<E> node = node(index);
        Node<E> prev = node.prev;
        Node<E> next = node.next;

        if(prev == null){ //删除头节点
            first = next;
        }else {
            prev.next = next;
        }

        if(next == null){
            last = prev;
        }else{
            next.prev = prev;
        }
        size--;
        return node.element;
    }

    @Override
    public int indexOf(E element) {
        Node<E> node = first; //从头结点开始遍历
        for(int i=0;i<size;++i){
            if(valEquals(element,node.element)) return i;
            node = node.next;
        }
        return ELEMENT_NOT_FOUND;
    }
    private boolean valEquals(Object v1,Object v2){
        return (v1 == v2) || (v1 != null && v1.equals(v2));
    }

    @Override
    public void clear() {
        size = 0;
        first = null;
        last = null;

    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(String.format("LinkedList:%d [",size)); //只有元素个数
        if(isEmpty()){
            sb.append(']');
        }else {
            Node<E> p =first; //代替头指针去遍历链表
            while(true){
                sb.append(p); //拼接数据
                if(p.next==null){ //如果到达了最后一个结点
                    sb.append(']');
                    break;
                }
                //不是最后一个结点，就拼接,号，并向后移
                sb.append(',');
                p=p.next;
            }
        }
        return sb.toString();
    }
}
